package com.shadow.bus.main.history;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.shadow.bus.adapter.ExpandableItemAdapter;
import com.shadow.bus.config.ServiceUrl;
import com.shadow.bus.config.SpKey;
import com.tepth.shadow.ui.dialog.sweet.DialogDelegate;
import com.tepth.shadow.ui.dialog.sweet.SweetAlertDialogDelegate;
import com.tepth.shadow.ui.recycler.BaseDecoration;
import com.tepth.shadow.ui.recycler.RecyclerViewNoBugLinearLayoutManager;
import com.zhangyu.shadow.core.delegate.RxShadowDelegate;
import com.zhangyu.shadow.core.net.callback.IError;
import com.zhangyu.shadow.core.net.callback.ISuccess;
import com.zhangyu.shadow.core.net.rx.RxRestClient;
import com.zhangyu.shadow.core.util.storage.ShadowPreference;

import java.util.ArrayList;

import busfix.tepth.com.shadow_bus.R;
import busfix.tepth.com.shadow_bus.R2;
import butterknife.BindView;

/**
 * @author zhangyu
 * @date 2018/7/9
 */

public class HistoryDelegate extends RxShadowDelegate {
    private static final String BUS_ID = "busCode";

    public static HistoryDelegate newInstance(String busCode) {
        HistoryDelegate fragment = new HistoryDelegate();
        Bundle bundle = new Bundle();
        bundle.putString(BUS_ID, busCode);
        fragment.setArguments(bundle);
        return fragment;
    }

    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private ExpandableItemAdapter mAdapter;
    private DialogDelegate mDialogDelegate;
    private String mBusCode;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mBusCode = bundle.getString(BUS_ID);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_history;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        mToolbar.setTitle("维保历史");
        mRecyclerView.setLayoutManager(new RecyclerViewNoBugLinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(BaseDecoration.create(getContext().
                getResources().getColor(R.color.color_white), 1));
        mAdapter = new ExpandableItemAdapter(null);
        mAdapter.setEmptyView(R.layout.view_load_data, (ViewGroup) mRecyclerView.getParent());
        mRecyclerView.setAdapter(mAdapter);
        mDialogDelegate = new SweetAlertDialogDelegate(getActivity());
        requestData();
    }

    private void requestData() {
        mDialogDelegate.showProgressDialog(false, "正在加载...");
        mDisposables.add(RxRestClient.builder()
                .url(ServiceUrl.REPAIR_HISTORY)
                .params("authKey", ShadowPreference.getCustomAppProfile(SpKey.TOKEN))
                .params("vehicleCode", mBusCode)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        HistoryDataConverter dataConverter = new HistoryDataConverter();
                        dataConverter.convert(response, new HistoryDataConverter.OnConverterListener() {
                            @Override
                            public void onConverter(ArrayList<MultiItemEntity> list) {
                                try {
                                    mAdapter.setEmptyView(R.layout.view_empty, (ViewGroup) mRecyclerView.getParent());
                                    mAdapter.setNewData(list);
                                    mDialogDelegate.clearDialog();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        mDialogDelegate.stopProgressWithFailed("加载失败", msg);
                    }
                })
                .build()
                .post());
    }

    @Override
    public boolean onBackPressedSupport() {
        pop();
        return true;
    }
}
